<?php
namespace app\resource\controller;
use app\common\controller\HomeCommon;
use think\Request;

/**
 * 
 * @author admin
 *
 */
class Companys extends HomeCommon
{
    public function index()
    {
    }
    /**
     * 企业列表
     */
    public function getlists(){
    	if(Request::instance()->isPost()){
    		$keyword=input('keyword');
    	
    		$page=empty(input('page'))?1:input('page');
    		
    		$where=" 1=1 and status=1 ";
    		if($keyword){
    			$where.=" and com_name like '%$keyword%'";
    		}
    		$list=db('companys')->field('id,com_name,logo')->where($where)->order('id desc')->paginate();
    		return json(['errcode'=>'0','msg'=>'获取成功！','data'=>$list]);
    	}
    }
    /**
     * 添加企业
     */
    public function addcompany(){
    	if(Request::instance()->isPost()){
    		$content=input('content');
    		if(empty($content)){
    			return json(['errcode'=>'10001','msg'=>'内容不能为空！','data'=>[]]);
    		}else{
    			
    			//判断企业是否存在
    			$info=db('companys')->field('id,com_name')->where(['com_name'=>$content,'status'=>'1'])->find();
    			if(!empty($info)){
    				return json(['errcode'=>'10002','msg'=>'企业已存在！','data'=>$info]);
    			}
    		}
    		$data['com_name']=$content;
    		$data['status']=0;
    		$result=db('companys')->insertGetId($data);
    		if($result){
    			$record['company_id']=$result;
    			$token=input('token');
    			
    			$result=$this->check_token($token);
    			
    			if($result['errcode']!=0){
    				$record['user_id']=$this->user_id;
    			}
    			$record['status']=1;
    			
    			$result=db('companys_addrecord')->insert($record);
    			
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    	}
    }
   
   
}
